package org.example;

import instrument.LogUtil;
import product.*;

import java.util.*;
import java.text.SimpleDateFormat;

public class ReceptionService {
    private User current;
    private final ManagerService manager;
    private final Scanner sc = new Scanner(System.in);
    private final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");

    public ReceptionService(ManagerService manager) {
        this.manager = manager;
    }

    public void setCurrentReception(User u) {
        current = u;
    }

    public boolean isLoggedIn() {
        return current != null;
    }

    public void saveData() {
        manager.saveData();
    }

    public void showReceptionMenu() {
        while (isLoggedIn()) {
            System.out.println("\n=== 前台菜单 ===");
            System.out.println("1.列出所有影片 2.列出所有场次 3.售票 4.退票 5.退出");
            switch (sc.nextLine().trim()) {
                case "1" -> manager.getMovies().forEach(Movie::display);
                case "2" -> listShows();
                case "3" -> sellTicket();
                case "4" -> refundTicket();
                case "5" -> {
                    logout();
                    return;
                }
                default -> System.out.println("无效选择");
            }
        }
    }

    private void listShows() {
        System.out.println("\n=== 所有场次 ===");
        manager.getShows().forEach(s -> System.out.printf("ID:%d 影片:%s 厅:%s 时间:%s 票价:%.2f%n",
                s.getId(), s.getMovie().getName(), s.getHall(),
                sdf.format(s.getTime()), s.getPrice()));
    }

    private void sellTicket() {
        System.out.print("场次ID: ");
        Show show = manager.findShowById(Integer.parseInt(sc.nextLine()));
        if (show == null) {
            System.out.println("场次不存在");
            return;
        }
        show.displaySeats();
        System.out.print("座位(行,列): ");
        String[] p = sc.nextLine().split(",");
        Seat seat = new Seat(Integer.parseInt(p[0]), Integer.parseInt(p[1]));
        if (!show.checkSeatAvailability(seat.getRow(), seat.getCol())) {
            System.out.println("座位不可用");
            return;
        }
        show.occupySeats(Set.of(seat));
        SaleRecord sr = new SaleRecord(UUID.randomUUID().toString().substring(0, 16), show, seat, show.getPrice(), new Date());
        manager.addSaleRecord(sr);
        manager.saveData();
        LogUtil.log(current.getUsername(), "售出票: " + sr.getTicketId());
        System.out.println("售票成功！票号: " + sr.getTicketId());
    }

    private void refundTicket() {
        System.out.print("票号: ");
        String id = sc.nextLine();
        SaleRecord sr = manager.getSaleRecords().stream()
                .filter(r -> r.getTicketId().equals(id) && !r.isRefunded())
                .findFirst().orElse(null);
        if (sr == null) {
            System.out.println("票号不存在或已退");
            return;
        }
        // 检查是否在放映前1小时内
        if (new Date().getTime() - sr.getShow().getTime().getTime() < 1 * 60 * 60 * 1000
                && new Date().getTime() < sr.getShow().getTime().getTime()) {
            System.out.println("电影上映前1小时无法退票");
            return;
        }
        sr.setRefunded(true);
        sr.setRefundTime(new Date());
        sr.setRefundAmount(sr.getPrice());
        sr.getShow().releaseSeat(sr.getSeat());
        manager.saveData();
        LogUtil.log(current.getUsername(), "退票: " + id);
        System.out.println("退票成功！");
    }

    public void logout() {
        current = null;
        System.out.println("已退出");
    }
}